package com.ruoyi.common.core.utils.send;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;


import javax.annotation.PostConstruct;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.MessageFormat;


@Component
public class SendUtils implements InitializingBean{


    @Autowired
    private JavaMailSender javaMailSender;


    private static JavaMailSender sendObj;



    public static boolean sendEmailCode(String from, String to,String title,String content) throws Exception {
        try {
            // 用来设置邮件信息
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            // 设置邮件标题
            simpleMailMessage.setSubject(title);
            // 设置邮件内容
            simpleMailMessage.setText(content);
            // 设置源邮箱
            simpleMailMessage.setFrom(from);
            // 设置目的邮箱
            simpleMailMessage.setTo(to);
            // 发送
            sendObj.send(simpleMailMessage);
            // 没有出现异常，正常发送，返回true
            return true;
        } catch (MailException e) {
            // 发送过程中，发生错误，打印错误信息，返回false
            e.printStackTrace();
            throw new Exception();
        }
    }

    /**
     * 向用户邮箱发送短信
     *
     * @param
     */
    public static void sendHtmlEmail(String from,String to,String title,String code) throws Exception{
        MimeMessage message = sendObj.createMimeMessage();
        //邮箱发送内容组成
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setSubject(title);
        Object[] objects=new Object[5];
        objects[0]=code;
        objects[1]="您好！感谢您使用第二课堂活动平台，您正在进行邮箱验证，验证码为：";
        objects[2]="，有效期5分钟，请尽快填写验证码完成验证！";
        objects[3]="Hello! Thank you for using the second class activity platform. You are performing mailbox verification. The verification code is:";
        objects[4]=". The validity period is 5 minutes. Please fill in the verification code as soon as possible to complete the verification!";
        helper.setText(buildContentArray(objects), true);
        helper.setTo(to);
        helper.setFrom(from);
        sendObj.send(message);
    }

    public static void sendHtmlEmailByUser(String from,String to,String title,Object[] objects) throws Exception {
        MimeMessage message = sendObj.createMimeMessage();
        //邮箱发送内容组成
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setSubject(title);
        helper.setText(buildContentArray(objects), true);
        helper.setTo(to);
        helper.setFrom(from);
        sendObj.send(message);
    }



    /**
     * 读取邮件模板
     * 替换模板中的信息
     *
     * @param title 内容
     * @return
     */
    public static String buildContent(String title) throws Exception {
        //加载邮件html模板
        Resource resource = new ClassPathResource("send.html");
        InputStream inputStream = null;
        BufferedReader fileReader = null;
        StringBuffer buffer = new StringBuffer();
        String line = "";
        try {
            inputStream = resource.getInputStream();
            fileReader = new BufferedReader(new InputStreamReader(inputStream));
            while ((line = fileReader.readLine()) != null) {
                buffer.append(line);
            }
        } catch (Exception e) {
            throw new Exception("解析模板失败");
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //替换html模板中的参数
        return MessageFormat.format(buffer.toString(), title);
    }
    public static String buildContentArray(Object[] objects) throws Exception {
        //加载邮件html模板
        Resource resource = new ClassPathResource("send.html");
        InputStream inputStream = null;
        BufferedReader fileReader = null;
        StringBuffer buffer = new StringBuffer();
        String line = "";
        try {
            inputStream = resource.getInputStream();
            fileReader = new BufferedReader(new InputStreamReader(inputStream));
            while ((line = fileReader.readLine()) != null) {
                buffer.append(line);
            }
        } catch (Exception e) {
            throw new Exception("解析模板失败");
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        //替换html模板中的参数
        return MessageFormat.format(buffer.toString(), objects);
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        sendObj=this.javaMailSender;
    }
}
